﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;
using System.Collections.ObjectModel;
using EIA.PM.Model;

namespace EIA.SLClient.ProjectManage.Progress
{
    public class DelayApplyViewModel : SimpleMvvmToolkit.ViewModelBase<DelayApplyViewModel>
    {
        public DelayApplyViewModel()
        {

        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected ObservableCollection<EIA.PM.Model.LProjectDelayApply> _DelayApplyCollection;
        /// <summary>
        /// 延期申请记录
        /// </summary>
        public ObservableCollection<EIA.PM.Model.LProjectDelayApply> DelayApplyCollection
        {
            get { return _DelayApplyCollection; }
            set
            {
                _DelayApplyCollection = value;
                NotifyPropertyChanged(m => m.DelayApplyCollection);
            }
        }

        protected EIA.PM.Model.TProjectBasic _ProjectBasic;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic ProjectBasic
        {
            get { return _ProjectBasic; }
            set
            {
                _ProjectBasic = value;
                NotifyPropertyChanged(m => m.ProjectBasic);
            }
        }
        #endregion

        #region command

        #region RetrieveItemsCommand

        private SimpleMvvmToolkit.DelegateCommand _RetrieveItemsCommand = null;
        public SimpleMvvmToolkit.DelegateCommand RetrieveItemsCommand
        {
            get
            {
                if (_RetrieveItemsCommand == null)
                {
                    _RetrieveItemsCommand = new SimpleMvvmToolkit.DelegateCommand(RetrieveItems, CanExcuteRetrieveItems);
                } return _RetrieveItemsCommand;
            }
            set { _RetrieveItemsCommand = value; NotifyPropertyChanged(m => m.RetrieveItemsCommand); }
        }

        public void RetrieveItems()
        {
            EIA.PM.BLL.ClientProxy.LProjectDelayApplyProxy proxy = new PM.BLL.ClientProxy.LProjectDelayApplyProxy();
            Busy();
            Dictionary<string, object> con = new Dictionary<string, object>();
            con[EIA.PM.Model.LProjectDelayApply.PRJ_ID] = ProjectBasic.PrjId;
            proxy.List(con, int.MaxValue, 1, (lst) =>
            {
                Free();
                DelayApplyCollection = new ObservableCollection<PM.Model.LProjectDelayApply>(lst);
            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("获取延期记录发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteRetrieveItems()
        {
            return true;
        }

        #endregion

        #region InsertItemCommand

        private SimpleMvvmToolkit.DelegateCommand _InsertItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand InsertItemCommand
        {
            get
            {
                if (_InsertItemCommand == null)
                {
                    _InsertItemCommand = new SimpleMvvmToolkit.DelegateCommand(InsertItem, CanExcuteInsertItem);
                } return _InsertItemCommand;
            }
            set { _InsertItemCommand = value; NotifyPropertyChanged(m => m.InsertItemCommand); }
        }

        public void InsertItem()
        {
            LProjectDelayApply pm = new LProjectDelayApply()
            {
                PrjId = ProjectBasic.PrjId,
                OrginalScheduled = ProjectBasic.PrjScheduled.HasValue ? ProjectBasic.PrjScheduled.Value : DateTime.Now
            };
            SendMessage<object, EIA.Basic.Model.TStaff>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.LOGIN_STAFF_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, EIA.Basic.Model.TStaff>("", null, (s) =>
            {
                pm.Staff = s == null ? null : (int?)s.StfId;
            }));
            if (DelayApplyCollection == null)
            {
                DelayApplyCollection = new System.Collections.ObjectModel.ObservableCollection<LProjectDelayApply>();
            }
            DelayApplyCollection.Insert(0, pm);
            this.SendMessage<LProjectDelayApply>(InnerMessageTokens.NEW, new SimpleMvvmToolkit.NotificationEventArgs<LProjectDelayApply>("", pm));
        }

        public bool CanExcuteInsertItem()
        {
            return true;
        }

        #endregion

        #region UpdateItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.LProjectDelayApply> _UpdateItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.LProjectDelayApply> UpdateItemCommand
        {
            get
            {
                if (_UpdateItemCommand == null)
                {
                    _UpdateItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.LProjectDelayApply>(UpdateItem, CanExcuteUpdateItem);
                } return _UpdateItemCommand;
            }
            set { _UpdateItemCommand = value; NotifyPropertyChanged(m => m.UpdateItemCommand); }
        }

        public void UpdateItem(EIA.PM.Model.LProjectDelayApply p)
        {
            if (p.ApproveState != null)
            {
                System.Windows.MessageBox.Show("无法修改已批复的延期申请！", "提示", System.Windows.MessageBoxButton.OK);
                return;
            }
            this.SendMessage<LProjectDelayApply>(InnerMessageTokens.UPDATE, new SimpleMvvmToolkit.NotificationEventArgs<LProjectDelayApply>("", p));
        }

        public bool CanExcuteUpdateItem(EIA.PM.Model.LProjectDelayApply p)
        {
            return true;
        }

        #endregion

        #region DeleteItemCommand

        private SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.LProjectDelayApply> _DeleteItemCommand = null;
        public SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.LProjectDelayApply> DeleteItemCommand
        {
            get
            {
                if (_DeleteItemCommand == null)
                {
                    _DeleteItemCommand = new SimpleMvvmToolkit.DelegateCommand<EIA.PM.Model.LProjectDelayApply>(DeleteItem, CanExcuteDeleteItem);
                } return _DeleteItemCommand;
            }
            set { _DeleteItemCommand = value; NotifyPropertyChanged(m => m.DeleteItemCommand); }
        }

        public void DeleteItem(EIA.PM.Model.LProjectDelayApply p)
        {
            if (p.ApproveState != null)
            {
                System.Windows.MessageBox.Show("无法删除已批复的延期申请！", "提示", System.Windows.MessageBoxButton.OK);
                return;
            }
            if (System.Windows.MessageBox.Show(string.Format("确认删除延期申请 [{0}] 天？", p.DlyDays), "提示", System.Windows.MessageBoxButton.OKCancel) != System.Windows.MessageBoxResult.OK) return;

            EIA.PM.BLL.ClientProxy.LProjectDelayApplyProxy proxy = new EIA.PM.BLL.ClientProxy.LProjectDelayApplyProxy();
            Busy();
            proxy.Delete(p.DlyId, (r) =>
            {
                Free();
                if (r)
                {
                    DelayApplyCollection.Remove(p);
                }
                else
                {
                    System.Windows.MessageBox.Show("确认删除延期申请失败！", "提示", System.Windows.MessageBoxButton.OK);
                }

            }, (ex) =>
            {
                Free();
                System.Windows.MessageBox.Show("确认删除延期申请发生错误！", "提示", System.Windows.MessageBoxButton.OK);
            });
        }

        public bool CanExcuteDeleteItem(EIA.PM.Model.LProjectDelayApply p)
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                ProjectBasic = p;
            }));
            RetrieveItems();
        }
    }
}